import 'dayjs/locale/zh-cn'
import dayjs from 'dayjs'
import md5 from 'js-md5'
dayjs.locale('zh-cn')
export const genNo = (randLen = 6) => {
  let cd = dayjs().format('YYYYMMDDHHmmss')
  let rs = 'QWERTYUIOPASDFGHJKLZXCVBNM'
  let t = []
  for (let i = 1; i <= randLen; i++) {
    let rn = random(rs.length - 1)
    t.push(rs[rn])
  }
  return cd + t.join('')
}

export const genCmptNo = (mix = '', randLen = 10) => {
  let cd = new Date().getTime()
  let rs = 'QWERTYUIOPASDFGHJKLZXCVBNM'
  let t = []
  for (let i = 1; i <= randLen; i++) {
    let rn = random(rs.length - 1)
    t.push(rs[rn])
  }
  return md5(t.join('') + cd + mix)
}

export const timeName = (time, format = '') => {
  let ot = dayjs(time)
  if (!dayjs.isDayjs(ot)) return ''
  if (format) {
    return ot.format(format)
  }
  let ct = dayjs()
  let cd = ct.format('YYYY-MM-DD')
  let yd = ct.subtract(1, 'days').format('YYYY-MM-DD')
  let qd = ct.subtract(2, 'days').format('YYYY-MM-DD')
  let od = ot.format('YYYY-MM-DD')
  let cy = ct.format('YYYY')
  let oy = ot.format('YYYY')

  if (cd === od) {
    return ot.format('HH:mm')
  }

  if (yd === od) {
    return '昨天' + ot.format('HH:mm')
  }

  if (qd === od) {
    return '前天 ' + ot.format('HH:mm')
  }

  if (isThisWeek(ot)) {
    return '本' + ot.format('ddd HH:mm')
  }

  if (isPrevWeek(ot)) {
    return '上' + ot.format('ddd HH:mm')
  }

  if (cy === oy) {
    return ot.format('MM月DD日 HH:mm')
  }

  return ot.format('YY年MM月DD日 HH:mm')
}

export const str2md5 = (str) => {
  return md5(str)
}

export const isThisWeek = (date) => {
  let td = dayjs()
  let isDayjs = dayjs.isDayjs(date)
  date = isDayjs ? date : dayjs(date)

  let thisDate = td.format('YYYY-MM-DD')
  let thisWeek = parseInt(dayjs().format('d'))
  let thisWeekStartDay = dayjs(thisDate + ' 00:00:00').subtract(thisWeek, 'days').format('YYYY-MM-DD')
  let thisWeekEndDay = dayjs(thisDate + ' 00:00:00').add(7 - thisWeek + 1, 'days').format('YYYY-MM-DD')

  if (date.isAfter(thisWeekEndDay)) return false
  if (date.isBefore(thisWeekStartDay)) return false
  return true
}

export const isNextWeek = (date) => {
  let td = dayjs()
  let isDayjs = dayjs.isDayjs(date)
  date = isDayjs ? date : dayjs(date)

  let thisDate = td.format('YYYY-MM-DD')
  let thisWeek = parseInt(dayjs().format('d'))
  let thisWeekStartDay = dayjs(thisDate + ' 00:00:00').subtract(thisWeek - 1, 'days').add(1, 'week').format('YYYY-MM-DD')
  let thisWeekEndDay = dayjs(thisDate + ' 00:00:00').add(7 - thisWeek + 1, 'days').add(1, 'week').format('YYYY-MM-DD')

  if (date.isBefore(thisWeekStartDay)) return false
  if (date.isAfter(thisWeekEndDay)) return false
  return true
}

export const isPrevWeek = (date) => {
  let td = dayjs()
  let isDayjs = dayjs.isDayjs(date)
  date = isDayjs ? date : dayjs(date)

  let thisDate = td.format('YYYY-MM-DD')
  let thisWeek = parseInt(dayjs().format('d'))
  let thisWeekStartDay = dayjs(thisDate + ' 00:00:00').subtract(thisWeek - 1, 'days').subtract(1, 'week')
  let thisWeekEndDay = dayjs(thisDate + ' 00:00:00').add(7 - thisWeek + 1, 'days').subtract(1, 'week')

  if (date.isBefore(thisWeekStartDay)) return false
  if (date.isAfter(thisWeekEndDay.subtract(1, 'second'))) return false
  return true
}

export const pad0 = (num, length) => {
  return (Array(length).join('0') + num).slice(-length)
}

export const random = (max) => {
  return Math.floor(Math.random() * Math.floor(max))
}

export const keyBy = (collection, name) => {
  let t = {}
  let keys = []
  collection.forEach(n => {
    keys.push(n[name])
  })
  keys.forEach(n => {
    t[n] = collection[keys.indexOf(n)]
  })
  return t
}

export const ltrim = (str, char) => {
  let regex = new RegExp('^[' + char + ']+', 'gi')
  return str.replace(regex, '')
}
export const rtrim = (str, char) => {
  let regex = new RegExp('[' + char + ']+$', 'gi')
  return str.replace(regex, '')
}

export const removeElFromArr = (arr, el) => {
  let idx = arr.indexOf(el)
  if (idx < 0) return arr
  arr.splice(idx, 1)
  return arr
}

export const toggleElFromArr = (arr, el) => {
  let idx = arr.indexOf(el)
  if (idx < 0) {
    arr.push(el)
    return arr
  }
  let t = removeElFromArr(arr, el)
  return t
}
